﻿#pragma once
#include"HashTable.h"

namespace zhz
{
	template<class K,class V, class Hash = HashFunc<K>>
	class unordered_map
	{
		struct MapkeyofT
		{
			const K& operator()(const pair<K,V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename hash_bucket::HashTable<K, pair<K,V>, Hash, MapkeyofT>::Iterator iterator;
		typedef typename hash_bucket::HashTable<K, pair<K, V>, Hash, MapkeyofT>::Const_Iterator const_iterator;

		iterator begin()
		{
			return _ht.Begin();
		}


		iterator end()
		{
			return _ht.End();
		}

		const_iterator begin()const
		{
			return _ht.Begin();
		}


		const_iterator end()const
		{
			return _ht.End();
		}

		pair<iterator, bool> insert(const pair<K,V> kv)
		{
			return _ht.Insert(kv);
		}
		iterator find(const K& key)
		{
			return _ht.Find(key);
		}


		bool erase(const K& key)
		{
			return _ht.Erase(key);
		}

		V& operator[](const K& key)
		{
			pair < iterator, bool > ret = _ht.Insert({ key,V() });
			return ret.first->second;
		}





	private:
		hash_bucket::HashTable<K, pair<K,V>, Hash, MapkeyofT> _ht;
	};


	void Print(const unordered_map<string, string>& dict)
	{
		unordered_map<string, string>::const_iterator it = dict.begin();
		while (it != dict.end())
		{
			//it->first += 'x';
			//it->second += 'x';

			cout << it->first << " " << it->second;
			++it;
			cout << endl;
		}
		cout << endl;
	}

	void test_map1()
	{

		string arr[] = { "苹果", "西瓜", "苹果", "樱桃", "苹果", "樱桃", "苹果", "樱桃", "苹果" };

		unordered_map<string, int> countMap;
		for (const auto& str : arr)
		{
			countMap[str]++;
		}

		for (const auto& e : countMap)
		{
			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;


		unordered_map<string, string> dict;
		dict.insert({ "sort", "排序" });
		dict.insert({ "left", "左边" });
		dict.insert({ "right", "右边" });
		dict["left"] = "左边，剩余";
		dict["insert"] = "插入";
		dict["string"];

		Print(dict);
	}
}